home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmiSoft / Comm / tcp / JabberwockySRC.lha / Jabberwocky / src / userinfo.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-04-28  |  9.0 KB  |  322 lines

  1. /*  Copyright (C) 2002 Tom Parker (tom@carrott.org),
  2.                        Matthias Münch (matthias@amigaworld.de)
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2 of the License, or
  7.     (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  17.  
  18.     In addition, as a special exception, Tom Parker and Matthias Münch give
  19.     permission to link the code of this program with a TCP stack of your 
  20.     choice, any official MUI libraries or classes and any custom MUI classes
  21.     that should be necessary for the operation of this program.  This 
  22.     exception also gives you permission to distribute linked combinations 
  23.     including this software with any of the before-mentioned libraries and 
  24.     classes.  You must obey the GNU General Public License in all respects for
  25.     all of the code used other than that provided by the before-mentioned 
  26.     libraries and classes.  As part of this exception you are obliged to 
  27.     follow the license terms of the before-mentioned libraries, this license
  28.     does not compel you to follow those terms, but if you do not then you may
  29.     not link with those libraries.  If you modify this file, you may extend
  30.     this exception to your version of the file, but you are not obligated to
  31.     do so.  If you do not wish to do so, delete this exception statement from
  32.     your version.
  33. */
  34. /*
  35. ** user information window
  36. */
  37.  
  38. #include "common.h"
  39.  
  40. #include <MUI/NListview_mcc.h>
  41. #include <MUI/NFloattext_mcc.h>
  42.  
  43. static ULONG userinfo_new(struct IClass *cl, Object *obj, struct opSet *msg);
  44. static void userinfo_showver(struct userinfodata *data, ikspak *pak);
  45.  
  46.  
  47. MUI_DISPATCH(userinfo_dispatch)
  48. {
  49.     switch(msg->MethodID)
  50.     {
  51.         case OM_NEW:
  52.             return userinfo_new(cl,obj,(APTR)msg);
  53.  
  54.         case USERINFO_UPDATE:
  55.         {
  56.             struct userinfodata *data = INST_DATA(cl,obj);
  57.             int parts;
  58.             char *tmp;
  59.             iks *x, *y;
  60.             tmp = mui_sget(data->namestr);
  61.             x = iks_make_iq(IKS_TYPE_SET, IKS_NS_ROSTER);
  62.             y = iks_insert(iks_child(x), "item");
  63.             
  64.             // When doing an update we want to refer to the user without their 
  65.             // resource specification - a user only has one name, not a name for 
  66.             // each resource.
  67.             // 
  68.             // However agents must be referred to using their full JID, including
  69.             // resource or strange things happen to your roster.
  70.             if (data->u->id->user == NULL) { 
  71.                 parts = IKS_ID_USER | IKS_ID_SERVER | IKS_ID_RESOURCE;
  72.             } else {
  73.                 parts = IKS_ID_USER | IKS_ID_SERVER;
  74.             }
  75.             
  76.             iks_insert_attrib(y, "jid", iks_id_printx(data->u->id, parts));
  77.             if(tmp) iks_insert_attrib(y, "name", convert_utf8(tmp));
  78.             iks_send(net.parser, x);
  79.             iks_delete(x);
  80.             return 0;
  81.         }
  82.  
  83.         case USERINFO_ASKVER:
  84.         {
  85.             struct userinfodata *data = INST_DATA(cl,obj);
  86.             ikspak *pak = (ikspak *)MARG1;
  87.  
  88.             if(pak)
  89.             {
  90.                 userinfo_showver(data, pak);
  91.             }
  92.             else
  93.             {
  94.                 iks *x;
  95.                 x = iks_make_iq(IKS_TYPE_GET, IKS_NS_VERSION);
  96.                 iks_insert_attrib(x, "to", iks_id_print(data->u->id));
  97.                 iks_send(net.parser, x);
  98.                 iks_delete(x);
  99.             }
  100.             return 0;
  101.         }
  102.  
  103.         case USERINFO_CLOSE:
  104.             ((struct userinfodata *)INST_DATA(cl,obj))->u->infowin = NULL;
  105.             DoMethod(gui.app, MUIM_Application_PushMethod, gui.list, 2, ROSTER_CLOSEME, obj);
  106.             return 0;
  107.     }
  108.     return DoSuperMethodA(cl,obj,msg);
  109. }
  110.  
  111.  
  112. static ULONG userinfo_new(struct IClass *cl, Object *obj, struct opSet *msg)
  113. {
  114.     static const char *pages[] =
  115.     {
  116.         "Presence",
  117.         "Client",
  118.         NULL
  119.     };
  120.     static const char *subsents[] =
  121.     {
  122.         "None",
  123.         "To",
  124.         "From",
  125.         "Both",
  126.         NULL
  127.     };
  128.     struct userinfodata *data;
  129.     Object *namestr, *idtxt, *grouptxt, *subs, *subscyc, *stat, *note, *upbut, *canbut;
  130.     Object *clientstr, *verstr, *osstr, *verbut;
  131.     juser u;
  132.     char *tmp;
  133.  
  134.     obj = (Object *)DoSuperNew(cl,obj,
  135.         MUIA_Window_ID, MAKE_ID('U','I','N','F'),
  136.         MUIA_Window_Title, MSG_USERINFO_TITLE,
  137.         MUIA_HelpNode, "window-info",
  138.         WindowContents, VGroup,
  139.             Child, ColGroup(2),
  140.                 Child, Label2(MSG_USERINFO_NAME),
  141.                 Child, (ULONG) namestr = StringObject,
  142.                     StringFrame,
  143.                     MUIA_CycleChain, 1,
  144.                 End,
  145.                 Child, Label2(MSG_USERINFO_JID),
  146.                 Child, (ULONG) idtxt = TextObject,
  147.                     TextFrame,
  148.                     MUIA_Background, MUII_TextBack,
  149.                 End,
  150.                 Child, Label2(MSG_USERINFO_GROUP),
  151.                 Child, (ULONG) grouptxt = TextObject,
  152.                     TextFrame,
  153.                     MUIA_Background, MUII_TextBack,
  154.                 End,
  155.             End,
  156.             Child, RegisterGroup(pages),
  157.                 Child, ColGroup(2),
  158.                     Child, Label1(MSG_USERINFO_SUBS),
  159.                     Child, HGroup,
  160.                         Child, (ULONG) subs = TextObject,
  161.                             TextFrame,
  162.                             MUIA_Background, MUII_TextBack,
  163.                         End,
  164.                         Child, Label1(MSG_USERINFO_CHANGETO),
  165.                         Child, (ULONG) subscyc = CycleObject,
  166.                             MUIA_HorizWeight, 0,
  167.                             MUIA_CycleChain, 1,
  168.                             MUIA_Cycle_Entries, subsents,
  169.                         End,
  170.                     End,
  171.                     Child, Label1(MSG_USERINFO_STATUS),
  172.                     Child, (ULONG) stat = TextObject,
  173.                         TextFrame,
  174.                         MUIA_Background, MUII_TextBack,
  175.                     End,
  176.                     Child, VGroup,
  177.                         MUIA_HorizWeight, 0,
  178.                         Child, Label1(MSG_USERINFO_NOTE),
  179.                         Child, RectangleObject, End,
  180.                     End,
  181.                     Child, NListviewObject,
  182.                         MUIA_FixHeightTxt, "\n\n\n",
  183.                         MUIA_NListview_NList, (ULONG) note = NFloattextObject,
  184.                             ReadListFrame,
  185.                         End,
  186.                     End,
  187.                 End,
  188.                 Child, VGroup,
  189.                     Child, ColGroup(2),
  190.                         Child, Label1(MSG_USERINFO_CLIENT),
  191.                         Child, (ULONG) clientstr = TextObject,
  192.                             TextFrame,
  193.                             MUIA_Background, MUII_TextBack,
  194.                         End,
  195.                         Child, Label1(MSG_USERINFO_VERSION),
  196.                         Child, (ULONG) verstr = TextObject,
  197.                             TextFrame,
  198.                             MUIA_Background, MUII_TextBack,
  199.                         End,
  200.                         Child, Label1(MSG_USERINFO_OS),
  201.                         Child, (ULONG) osstr = TextObject,
  202.                             TextFrame,
  203.                             MUIA_Background, MUII_TextBack,
  204.                         End,
  205.                     End,
  206.                     Child, HGroup,
  207.                         Child, HSpace(0),
  208.                         Child, verbut = mui_button(MSG_USERINFO_ASKVER_GAD),
  209.                     End,
  210.                 End,
  211.             End,
  212.             Child, RectangleObject,
  213.                 MUIA_FixHeightTxt, "M",
  214.                 MUIA_Rectangle_HBar, TRUE,
  215.             End,
  216.             Child, HGroup,
  217.                 Child, upbut = mui_button(MSG_USERINFO_UPDATE_GAD),
  218.                 Child, canbut = mui_button(MSG_USERINFO_CLOSE_GAD),
  219.             End,
  220.         End,
  221.         TAG_MORE, msg->ops_AttrList);
  222.  
  223.     if(!obj) return(0);
  224.  
  225.     u = (juser)GetTagData(USERINFO_OF, NULL, msg->ops_AttrList);
  226.     set(namestr, MUIA_String_Contents, (ULONG) convert_locale(u->name));
  227.     set(idtxt, MUIA_Text_Contents, (ULONG) iks_id_print(u->id));
  228.     set(grouptxt, MUIA_Text_Contents, (ULONG) convert_locale(u->group));
  229.     set(note, MUIA_Floattext_Text, (ULONG) convert_locale(u->status));
  230.  
  231.     tmp = "None.";
  232.     switch(u->subs)
  233.     {
  234.     case JUSER_TO: tmp=MSG_USERINFO_SUBSCRIPTION_TO; break;
  235.     case JUSER_FROM: tmp=MSG_USERINFO_SUBSCRIPTION_FROM; break;
  236.     case JUSER_BOTH: tmp=MSG_USERINFO_SUBSCRIPTION_BOTH; break;
  237.     }
  238.     set(subs, MUIA_Text_Contents, (ULONG) tmp);
  239.  
  240.     tmp = "Available.";
  241.     switch(u->show)
  242.     {
  243.     case IKS_SHOW_UNAVAILABLE: tmp=MSG_USERINFO_STATUS_UNAVAILABLE; break;
  244.     case IKS_SHOW_CHAT: tmp=MSG_USERINFO_STATUS_CHAT; break;
  245.     case IKS_SHOW_AWAY: tmp=MSG_USERINFO_STATUS_TEMP_AWAY; break;
  246.     case IKS_SHOW_XA: tmp=MSG_USERINFO_STATUS_EXTENDED_AWAY; break;
  247.     case IKS_SHOW_DND: tmp=MSG_USERINFO_STATUS_DND; break;
  248.     }
  249.     set(stat, MUIA_Text_Contents, (ULONG) tmp);
  250.  
  251.     data = INST_DATA(cl,obj);
  252.     data->u = u;
  253.     data->namestr = namestr;
  254.     data->clientstr = clientstr;
  255.     data->verstr = verstr;
  256.     data->osstr = osstr;
  257.  
  258.     userinfo_showver(data, NULL);
  259.  
  260.     DoMethod(upbut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, USERINFO_UPDATE);
  261.     DoMethod(verbut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 2, USERINFO_ASKVER, 0);
  262.  
  263.     DoMethod(canbut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, USERINFO_CLOSE);
  264.     DoMethod(obj, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, obj, 1, USERINFO_CLOSE);
  265.  
  266.     return((ULONG)obj);
  267. }
  268.  
  269.  
  270. static void userinfo_showver(struct userinfodata *data, ikspak *pak)
  271. {
  272.     iks *x;
  273.  
  274.     if(pak)
  275.     {
  276.         if(data->u->verpak) iks_delete(data->u->verpak->x);
  277.         data->u->verpak = pak;
  278.     }
  279.     else
  280.     {
  281.         pak = data->u->verpak;
  282.         if(!pak) return;
  283.     }
  284.  
  285.     x = iks_find(pak->x, "query");
  286.     set(data->clientstr, MUIA_Text_Contents, (ULONG) iks_find_cdata(x, "name"));
  287.     set(data->verstr, MUIA_Text_Contents, (ULONG) iks_find_cdata(x, "version"));
  288.     set(data->osstr, MUIA_Text_Contents, (ULONG) iks_find_cdata(x, "os"));
  289. }
  290.  
  291.  
  292. void userinfo_of(juser u)
  293. {
  294.     Object *win;
  295.  
  296.     if(u->infowin) {
  297.         DoMethod(u->infowin, MUIM_Window_ToFront);
  298.         return;
  299.     }
  300.  
  301.     win = NewObject(gui.userinfo_mcc->mcc_Class, NULL, USERINFO_OF, (ULONG) u, TAG_DONE);
  302.     if(win) {
  303.         DoMethod(gui.app, OM_ADDMEMBER, win);
  304.         set(win, MUIA_Window_Open, TRUE);
  305.         u->infowin = win;
  306.     }
  307. }
  308.  
  309.  
  310. void userinfo_update(ikspak *pak)
  311. {
  312.     juser u;
  313.  
  314.     u = roster_find(pak->from);
  315.     if(!u) return;
  316.  
  317.     if(u->infowin)
  318.         DoMethod(u->infowin, USERINFO_ASKVER, pak);
  319.     else
  320.         u->verpak = pak;
  321. }
  322.